class ProjectUtil {
  static showErrorToast(title: string, duration?: number) {
    return ProjectUtil.showToast({
      title,
      icon: 'none',
      duration,
    });
  }

  static showToast(option: UniApp.ShowToastOptions): Promise<void> {
    return new Promise((resolve, reject) => {
      uni.showToast(option);
      if (option.duration) {
        setTimeout(() => resolve(), option.duration);
      } else {
        resolve();
      }
    });
  }

  static sleep(time: number = 1000): Promise<void> {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve();
      }, time);
    });
  }

  static statusHeight() {
    const top = 0;
    try {
      const info = uni.getSystemInfoSync();
      return (info.statusBarHeight || 0) + top;
    } catch (error) {
      return top;
    }
  }

  static getWxTitleHeight() {
    // #ifdef MP
    if (uni.getMenuButtonBoundingClientRect) {
      let rect = uni.getMenuButtonBoundingClientRect();
      return rect.height;
    }
    // #endif

    return 0;
  }

  static emit(target: Vue, type: string, data?: any) {
    target.$emit(type, { detail: data });
  }

  static chooseImage(): Promise<string> {
    return new Promise((resolve, reject) => {
      uni.chooseImage({
        count: 1,
        success: (res) => {
          const fileList = res.tempFilePaths;
          if (fileList && fileList.length) {
            resolve(fileList[0]);
          } else {
            reject('未选择文件');
          }
        },
        fail: (result) => {
          reject(result);
        },
      });
    });
  }
}
export default ProjectUtil;
